
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:reabampos/src/reabam/operation/bean/login/request_login.dart';
import 'package:reabampos/src/reabam/operation/rb_api.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_home_index.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_login.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_login_utils.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
选择门店列表
 */
class PageStateSelfServiceSelectStoreList extends XPageListViewState{

  String? sword;
  String? currentCompanyId;

  PageStateSelfServiceSelectStoreList();

  @override
  void onStart() {
    currentCompanyId=XSharePreferencesUtils.getString(RBConfigs.SP_companyId);

    setXTopBarWidget(
      height: null,
      child: RBWidgets_SelfService.getTitleBarWidget(
          this,
          isShowSearchBar: true,
          initSearchKey: sword,
          hintSearchWord: '搜索门店名称',
          onChangedSearchBar: (res){
            sword=res;
          },
          onSubmitSearchBar: (res){
            updateData();
          }
      ),
    );
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {

    showLoading();
    RBBaseApi.getStaffStoreList(
        sword: sword,
        succeed: (response, Map<String, dynamic> responseMap, Map<String, dynamic> headers_response) {
          hideLoading();
          setXListData_addAllItems(responseMap['data']);

        },
        failed: (String code, String msg) {
          hideLoading();
          toast(msg);

        }
    );
  }

  @override
  Widget getListItemWidget(listItem, int position) {

    String imgName=listItem['staffName']??'';
    if(imgName.length>2){
      imgName=imgName.substring(0,2);
    }

    bool isSelect=false;
    if(currentCompanyId==listItem['companyId']){
      isSelect=true;
    }

    return XWidgetUtils.getWidgetGestureDetector(
      child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
          radius_bottomLeft: 16,
          radius_bottomRight: 16,
          radius_topRight: 16,
          radius_topLeft: 16,
          borderColor: isSelect?RBColors.reabamColor4:Colors.white,
          borderWidth: 3,
          padding: XWidgetUtils.getEdgeInsets(0, 0, 0, 0),
          margin: XWidgetUtils.getEdgeInsets(0, 10, 0, 10),
          height: 220,
          child:
          XWidgetUtils.getLayoutStack(
              children: [
                XWidgetUtils.getLayoutContainer(
                  alignment: Alignment.centerLeft,
                  height: 220,
                  padding: XWidgetUtils.getEdgeInsets(30, 0, 30, 0),
                  child: XWidgetUtils.getLayoutRow(
                      children: [
                        XWidgetUtils.getXLayoutCircle(
                            width:146,
                            height: 146,
                            backgroundColor: RBColors.reabamColor4,
                            margin: XWidgetUtils.getEdgeInsets(0, 0, 34, 0),
                            padding: XWidgetUtils.getEdgeInsets(2, 2, 2, 2),
                            alignment: Alignment.center,
                            child:XImageUtils.getImageWidgetWithUrl(
                                width: 144,
                                height: 144,
                                imageUrl: listItem['brandLogo'],
                                isCircle: true,
                                defaultImageAssetName: 'default_square.png')
                        ),
                        XWidgetUtils.getLayoutColumn(
                            shrinkWrap:true,
                            mainAxisAlignment:MainAxisAlignment.center,
                            children: [
                              XWidgetUtils.getWidgetText(
                                  '${listItem['companyName']??''}',
                                  style: XStyleUtils.getTextStyle(
                                      fontSize:34,
                                      color: XColorUtils.color333333,
                                      fontWeight: FontWeight.bold
                                  ),
                                  padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0)
                              ),
                              XWidgetUtils.getWidgetText(
                                  '角色 ${listItem['userTypeName']??''}',
                                  style: XStyleUtils.getTextStyle(
                                      fontSize:28,
                                      color: XColorUtils.color666666
                                  )
                              ),
                              XWidgetUtils.getWidgetText(
                                  '品牌 ${listItem['groupName']??''}',
                                  style: XStyleUtils.getTextStyle(
                                      fontSize:28,
                                      color: XColorUtils.color666666
                                  )
                              ),
                            ]
                        ),
                      ]
                  ),
                ),
                isSelect?XImageUtils.getImageWidgetWithAsset(
                    'ss_dangqian.png',
                  width: 83,
                  height: 84,
                ):XWidgetUtils.getLayoutContainer()
              ]
          )
      ),
      onTap: (){
        if(currentCompanyId!=listItem['companyId']){
          _changeCompany(listItem);
        }

      }
    );
  }


  void _changeCompany(Map<String,dynamic> item){
    showLoading();
    RBBaseApi.changePinPaiShangOrCompany(
      groupId: null,
      companyId: item['companyId']??'',
      succeed: (dynamic response1, Map<String, dynamic> responseMap, Map<String, dynamic> headers_response) {
        String? fid=responseMap['fid'];
        String? groupName=responseMap['groupName'];
        String? tokenId=responseMap['tokenId'];
        String? groupId=responseMap['groupId'];
        String? userCode=responseMap['userCode'];
        String? groupCode=responseMap['groupCode'];
        List<MenuBean>? menus=responseMap['menus'];
        List<FunConfigsBean>? funs=responseMap['funs'];

        XSharePreferencesUtils.saveString(RBConfigs.SP_tokenId,tokenId??'');
        XSharePreferencesUtils.saveString(RBConfigs.SP_MENUS,XJsonUtils.obj2Json(menus??[]));
        XSharePreferencesUtils.saveString(RBConfigs.SP_FUNS,XJsonUtils.obj2Json(funs??[]));
        XSharePreferencesUtils.saveString(RBConfigs.SP_GroupName,groupName??'');
        XSharePreferencesUtils.saveString(RBConfigs.SP_BrandLogo,item['brandLogo']??'');

        XSharePreferencesUtils.saveString(RBConfigs.SP_groupId, groupId??'');
        XSharePreferencesUtils.saveString(RBConfigs.SP_companyId, '');
        XSharePreferencesUtils.saveString(RBConfigs.SP_CompanyName, '');
        XSharePreferencesUtils.saveString(RBConfigs.SP_CompanyCode, '');
        XSharePreferencesUtils.saveString(RBConfigs.SP_userId, fid??'');

        //重新登录
        String username=XSharePreferencesUtils.getString(RBConfigs.SP_loginName);
        String password=XSharePreferencesUtils.getString(RBConfigs.SP_loginPwd);
        RBLoginUtils.login(
            this,
            loginName: username,
            loginPWD: password,
            groupId:groupId,
            onCallback: (res){

              //清除缓存
              XHttpUtils.cleanCache();

              //
              XNotifyStateChangeUtils.notifyStateChange(
                  action: RBConfigs.Notify_Global_Handler,
                  key: 'changeCompany',
                  value: ''
              );

              currentCompanyId=XSharePreferencesUtils.getString(RBConfigs.SP_companyId);

              updateData();

            }
        );

      },
      failed: (String code, String msg) {
        hideLoading();
        toast(msg);
      },
    );

  }

  @override
  EdgeInsets getListViewPadding() {
    return XWidgetUtils.getEdgeInsets(30, 10, 30, 10);
  }



}